package com.example.restatr.Atcivity.Main.Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.example.restatr.Adapter.CardAdapter;
import com.example.restatr.Atcivity.Bus.BusAt;
import com.example.restatr.Atcivity.CitySubway.CitySubwayAt;
import com.example.restatr.Atcivity.Hospital.HospitalAt;
import com.example.restatr.Atcivity.Main.MoreAt;
import com.example.restatr.Atcivity.LivePayment.LivePaymentAt;
import com.example.restatr.Atcivity.Traffic.TrafficAt;
import com.example.restatr.Bean.CardBean;
import com.example.restatr.R;
import com.example.restatr.Atcivity.Park.ParkAt;
import com.example.restatr.Tools;
import com.example.restatr.Atcivity.TakeOut.TakeOutAt;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    private Banner rbt;
    private TabLayout Tab;
    public static ViewPager XwPager;
    public static List<String> tabTitle = new ArrayList<>();
    public static List<String> tabId = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private GridView gridView;
    private List<CardBean> cardBeans = new ArrayList<>();
    private String get;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate( R.layout.fragment_home, container, false );

        rbt = view.findViewById( R.id.banner );
        Tab = view.findViewById( R.id.Tab );
        XwPager = view.findViewById( R.id.XwPager );
        gridView = view.findViewById( R.id.GridView );

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().replace( R.id.XwPager, new NewsFragment() ).commit();
        }
        //获取轮播图
        Tools.SetBanner("/prod-api/api/rotation/list?type=2", "", "rows", "advImg",rbt,getActivity());
        //获取新闻类别
        Tools.GetXwFl( "/prod-api/press/category/list", "",getActivity(),"data", tabTitle, tabId, fragments, XwPager, Tab, getFragmentManager() );
        //获取服务信息
        new Thread( new Runnable() {
            @Override
            public void run() {
                GetService();
                while (get == null) {
                    GetService();
                }
            }
        } ).start();

        Tab.setupWithViewPager( XwPager );

        gridView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (view.getTag().toString()) {
                    case "停哪儿":
                        startActivity( new Intent( getActivity(), ParkAt.class ) );
                        break;
                    case "城市地铁":
                        startActivity( new Intent( getActivity(), CitySubwayAt.class ) );
                        break;
                    case "智慧巴士":
                        Tools.StartActivity( getActivity(), BusAt.class );
                        break;
                    case "门诊预约":
                        startActivity( new Intent( getActivity(), HospitalAt.class ) );
                        break;
                    case "智慧交管":
                        startActivity( new Intent( getActivity(), TrafficAt.class ) );
                        break;
                    case "生活缴费":
                        startActivity( new Intent( getActivity(), LivePaymentAt.class ) );
                        break;
                    case "外卖订餐":
                        startActivity( new Intent( getActivity(), TakeOutAt.class ) );
                        break;
                    case "更多服务":
                        startActivity( new Intent( getActivity(), MoreAt.class ) );
                        break;
                }
            }
        } );

        return view;
    }

    private void GetService() {
        get = Tools.Get( "/prod-api/api/service/list", "" );
        if (get != null && getActivity() != null) {
            getActivity().runOnUiThread( new Runnable() {
                @Override
                public void run() {
                    try {
                        JSONObject jx = new JSONObject( get );
                        JSONArray rows = jx.getJSONArray( "rows" );
                        for (int i = 0; i < 7; i++) {
                            JSONObject all = rows.getJSONObject( i );
                            cardBeans.add( new CardBean( all.getString( "imgUrl" ), all.getString( "serviceName" ), all.getString( "id" ) ) );
                        }
                        cardBeans.add( new CardBean( "", "更多服务", "" ) );
                        gridView.setAdapter( new CardAdapter( "url", getActivity(), cardBeans ) );
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            } );
        }
    }


}